﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace _91cc.Main
{
    public partial class URLSet : DevExpress.XtraEditors.XtraForm
    {
        public URLSet()
        {
            InitializeComponent();
        }

        private void URLSet_Load(object sender, EventArgs e)
        {
            gC_data.DataSource = Config.RSSConfig.Instance.RssBases;
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            string caption = txt_caption.Text;
            string link = txt_link.Text;
            if (string.IsNullOrEmpty(caption))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Caption 不能为空!");
                txt_caption.Focus();
                return;
            }
            if (string.IsNullOrEmpty(link))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Link 不能为空!");
                txt_link.Focus();
                return;
            }
            Config.RssBase rssbase = new Config.RssBase();
            rssbase.Caption = caption;
            rssbase.Link = link;

            Config.RSSConfig.Instance.RssBases.Add(rssbase);
            Config.RSSConfig.SavaConfig(Config.RSSConfig.Instance);

            DevExpress.XtraEditors.XtraMessageBox.Show("添加成功!");

            gC_data.BeginInit();
            gC_data.DataSource = Config.RSSConfig.Instance.RssBases;
            gC_data.EndInit();
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            if (gV_data.GetSelectedRows().Length < 1)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("请在列表中选中您要删除的RSS!");
                return;
            }
            
            int[] index = gV_data.GetSelectedRows();
            foreach (int i in index)
            {
                string caption = gV_data.GetRowCellDisplayText(i, gridColumn1);
                Config.RSSConfig.DeleteItem(caption);
            }

            Config.RSSConfig.SavaConfig(Config.RSSConfig.Instance);

            gC_data.BeginInit();
            gC_data.DataSource = Config.RSSConfig.Instance.RssBases;
            gC_data.EndInit();
        }

        private void URLSet_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
        
    }
}
